package p.sunmes.les.util;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData;
import com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.SkeletonData;
import com.esotericsoftware.spine.SkeletonJson;

import p.sunmes.les.managers.GM;
import p.sunmes.les.managers.RM;

/**
 * Util
 */
public class U {

	public static Skeleton getSkeleton(String name) {
		SkeletonData sk = RM.instance().getSkeletons().get(name);
		if (sk == null) {
			String jsStr = String.format("%s.atlas", name);
			String dataStr = String.format("%s.json", name);
			TextureAtlas ta = new TextureAtlas(RM.getFile(jsStr));
			for (Texture tex : ta.getTextures()) {
				tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			}
			RM.instance().addDisposableCache(ta);
			SkeletonJson sj = new SkeletonJson(ta);
			sk = sj.readSkeletonData(RM.getFile(dataStr));
			RM.instance().getSkeletons().put(name, sk);
		}

		return new Skeleton(sk);
	}

	public static BitmapFont createPicFont(String name, String chars) {
		TextureRegion region = region(name);
		BitmapFontData data = new BitmapFontData();
		int width = region.getRegionWidth() / chars.length();
		for (int i = 0; i < chars.length(); i++) {
			Glyph g = new Glyph();
			char c = chars.charAt(i);
			g.id = c;
			g.srcX = i * width;
			g.xadvance = width;
			g.width = width;
			g.height = region.getRegionHeight();
			data.setGlyph(c, g);
		}
		BitmapFont font = new BitmapFont(data, region, false);
		return font;
	}

	public static TextureFilter usedTextureFilter = TextureFilter.Nearest;

	public static TextureRegion region(String name) {
		TextureRegion region;
		region = RM.instance().getLoad(name);
		Texture t = region.getTexture();
		if (t.getMagFilter() != usedTextureFilter
				|| t.getMinFilter() != usedTextureFilter) {
			t.setFilter(usedTextureFilter, usedTextureFilter);
		}
		return region;
	}

	public static Image image(String name) {
		return new Image(region(name));
	}

	public static void disTouch(Actor a) {
		a.setTouchable(Touchable.disabled);
	}

	public static void enaTouch(Actor a) {
		a.setTouchable(Touchable.enabled);
	}

	public static Image createMask() {
		Image img = new Image(RM.instance().getMaskTexture());
		img.setSize(GM.stage.getWidth(), GM.stage.getHeight());
		return img;
	}

	public static void stageGroup(Group g) {
		g.setSize(GM.stage.getWidth(), GM.stage.getHeight());

	}

	//	/**
	//	 * create fbo of group
	//	 * @param stage		for add fbo to disposableList.
	//	 * @param g			group to draw
	//	 * @param gAlign	align of group
	//	 * @return
	//	 */
	//	public static TextureRegion createCombine(Group g, int gAlign) {
	//		float width = g.getWidth();
	//		float height = g.getHeight();
	//		float x = g.getX();
	//		float y = g.getY();
	//		switch (gAlign) {
	//		case Align.bottom:
	//			g.setPosition(width / 2, 0);
	//			break;
	//		case Align.bottomLeft:
	//			g.setPosition(0, 0);
	//			break;
	//		case Align.bottomRight:
	//			g.setPosition(width, 0);
	//			break;
	//		case Align.center:
	//			g.setPosition(width / 2, height / 2);
	//			break;
	//		case Align.left:
	//			g.setPosition(0, height / 2);
	//			break;
	//		case Align.right:
	//			g.setPosition(width, height / 2);
	//			break;
	//		case Align.top:
	//			g.setPosition(width / 2, height);
	//			break;
	//		case Align.topLeft:
	//			g.setPosition(0, height);
	//			break;
	//		case Align.topRight:
	//			g.setPosition(width, height);
	//			break;
	//		}
	//		int w = (int) Math.ceil(width);
	//		int h = (int) Math.ceil(height);
	//		FrameBuffer fbo = new FrameBuffer(Format.RGBA8888, w, h, false);
	//		SpriteBatch batch = new SpriteBatch();
	//		fbo.begin();
	//		batch.begin();
	//		Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
	//				Gdx.graphics.getHeight());
	//		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
	//		g.draw(batch, 1);
	//		batch.end();
	//		fbo.end();
	//
	//		GM.viewport.apply(true);
	//
	//		Texture t = fbo.getColorBufferTexture();
	//		t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
	//		batch.dispose();
	//		//		fbo.dispose();
	//		GM.stage.addDisposeSign(fbo);
	//		g.setPosition(x, y);
	//
	//		TextureRegion region = new TextureRegion(t, 0, t.getHeight(),
	//				t.getWidth(), -t.getHeight());
	//
	//		return region;
	//	}
	//
	//	public static Image groupOnceBtnImage(String... pic) {
	//		Group g = new Group();
	//		for (String pi : pic) {
	//			Image img = image(pi);
	//			g.addActor(img);
	//			if (img.getWidth() > g.getWidth()) {
	//				g.setWidth(img.getWidth());
	//			}
	//			if (img.getHeight() > g.getHeight()) {
	//				g.setHeight(img.getHeight());
	//			}
	//		}
	//		for (Actor a : g.getChildren()) {
	//			a.setPosition(0, 0, Align.center);
	//		}
	//		TextureRegion region = createCombine(GM.stage, g, Align.center);
	//		Image img = new Image(region);
	//		return img;
	//	}

	public static void msg(String msg) {
		Label l = LU.getLabelWithBorder(msg);
		GM.stage.addActor(l);
		l.setAlignment(Align.center);
		l.setPosition(GM.stage.getWidth() / 2, GM.stage.getHeight() / 2,
				Align.center);
		l.addAction(
				Actions.sequence(Actions.parallel(Actions.moveBy(0, 100, 2f),
						Actions.fadeOut(3f)), Actions.removeActor()));
	}

	public static void debugErr(String msg) {
		debugMsg(msg, 5, Color.RED);
	}

	private static final String DEBUG_LABEL_NAME = "INFOLABELLL";
	private static final List<Actor> labelList = new ArrayList<Actor>();

	public static void debugMsg(String msg, final float delayTime,
			Color color) {
		System.out.println(msg);
		Label infoLabel = LU.getLabelWithBorder(msg);
		infoLabel.setName(DEBUG_LABEL_NAME);
		infoLabel.setColor(color);
		GM.stage.addActor(infoLabel);
		infoLabel.setWidth(GM.stage.getWidth());
		infoLabel.setWrap(true);
		infoLabel.setAlignment(Align.topLeft);

		infoLabel.addAction(new Action() {
			float dd = delayTime;

			@Override
			public boolean act(float delta) {
				dd -= delta;

				float yOff = 0;
				labelList.clear();
				boolean isSign = false;
				for (Actor a : GM.stage.getActors()) {
					if (a instanceof Label
							&& DEBUG_LABEL_NAME.equals(a.getName())) {
						if (a == target) {
							isSign = true;
						}
						if (!isSign) {
							yOff += a.getHeight();
						}
						labelList.add(a);
					}
				}
				target.setPosition(10, GM.stage.getHeight() - 10 - yOff,
						Align.topLeft);

				for (Actor a : labelList) {
					GM.stage.addActor(a);
				}

				if (dd < 0) {
					target.remove();
					return true;
				}
				return false;
			}
		});
	}

}
